//管理员功能
module.exports = function(app) {
  return new Handler(app)
}

var Handler = function(app) {
  this.app = app
  Handler.app = app
  this.sessionService = this.app.get('sessionService')
  this.channelService = this.app.get('channelService')
  this.gameChanel = this.channelService.getChannel("GameChannel",true)
}
var local = {}
var handler = Handler.prototype

//获取房间列表
handler.getRoomList = function(msg,session,next) {
	local.check(session,function(flag) {
		if(flag){
			Handler.app.rpc.game.remote.getRoomList(null,function(data) {
				next(null,{flag : true,msg : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}

//获取房间信息  本功能对所有人开放
handler.getRoomInfo = function(msg,session,next) {
	Handler.app.rpc.game.remote.getRoomInfo(null,msg.roomId,function(flag,data) {
		if(flag){
			next(null,{flag : true,msg : data})
		}else{
			next(null,{flag : false})
		}
	})
}
//获取在线玩家列表
handler.getOnlineUser = function(msg,session,next) {
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.game.remote.getOnlineUser(null,function(data) {
				next(null,{flag : true,msg : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//设置维护
handler.setMaintainFlag = function(msg,session,next) {
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.game.remote.setMaintainFlag(null,msg.flag,function(flag,data) {
				next(null,{flag : true,msg : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//获取所有比赛场信息
handler.getCompetitionAll = function(msg,session,next) {
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.game.remote.getCompetitionAll(null,function(flag,msg) {
				next(null,{flag : flag,msg : msg})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//获取比赛场详细信息
handler.getCompetitionInfoByNumeration = function(msg,session,next) {
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.game.remote.getCompetitionInfoByNumeration(null,msg.numeration,function(flag,msg) {
				next(null,{flag : flag,msg : msg})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//获取比赛场库存
handler.getCompetitionInventory = function(msg,session,next) {
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.game.remote.getCompetitionInventory(null,function(flag,msg) {
				next(null,{flag : flag,msg : msg})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//设置比赛场库存
handler.setCompetitionInventory = function(msg,session,next) {
	var cid = msg.cid
	var value = msg.value
	if(!cid || !value || typeof(value) != "number"){
		next(null,{flag : false,err : "参数错误"})
		return
	}
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.game.remote.setCompetitionInventory(null,cid,value,function(flag,msg) {
				next(null,{flag : flag,msg : msg})
			})
		}else{
			next(null,{flag : false})
		}
	})
}

//获取金币场库存
handler.getGoldInventory = function(msg,session,next) {
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.game.remote.getGoldInventory(null,function(flag,msg) {
				next(null,{flag : flag,msg : msg})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//设置金币场库存
handler.setGoldInventory = function(msg,session,next) {
	var rid = msg.rid
	var value = msg.value
	if(!rid || !value || typeof(value) != "number"){
		next(null,{flag : false,err : "参数错误"})
		return
	}
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.game.remote.setGoldInventory(null,rid,value,function(flag,msg) {
				next(null,{flag : flag,msg : msg})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//获取背包
handler.getBag = function(msg,session,next) {
	var uid = msg.uid
	if(!uid || typeof(uid) != "number" || uid < 0){
		next(null,{flag : false,err : "参数错误"})
		return
	}
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.bag.getBag(null,uid,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//更改物品
handler.changeBag = function(msg,session,next) {
	var uid = msg.uid
	var itemId = msg.itemId
	var value = msg.value
	if(!uid || typeof(uid) != "number" || uid < 0 || !itemId || typeof(itemId) != "number" || itemId < 0 || !value || typeof(value) != "number"){
		next(null,{flag : false,err : "参数错误"})
		return
	}
	var adminUid = session.get("uid")
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.remote.addItemReason(null,uid,itemId,value,"管理员更改:"+adminUid,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//获取总库存
handler.getTotalInventory = function(msg,session,next) {
	var adminUid = session.get("uid")
	var gameType = msg.gameType
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.admin.getTotalInventory(null,gameType,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//设置总库存
handler.setTotalInventory = function(msg,session,next) {
	var adminUid = session.get("uid")
	var gameType = msg.gameType
	var level = msg.level
	var value = msg.value
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.admin.setTotalInventory(null,adminUid,gameType,level,value,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//获取个人库存列表
handler.getSingleInventory = function(msg,session,next) {
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.admin.getSingleInventory(null,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//设置个人库存
handler.setSingleInventory = function(msg,session,next) {
	var adminUid = session.get("uid")
	var targetUid = msg.targetUid
	var value = msg.value
	if(!targetUid || typeof(targetUid) != "number" || !value || typeof(value) != "number"){
		next(null,{flag : false,err : "参数错误"})
		return
	}
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.admin.setSingleInventory(null,adminUid,targetUid,value,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//清空个人库存
handler.cancelSingleInventory = function(msg,session,next) {
	var adminUid = session.get("uid")
	var targetUid = msg.targetUid
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.admin.cancelSingleInventory(null,adminUid,targetUid,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//上下分
handler.changeDiamond = function(msg,session,next) {
	var adminUid = session.get("uid")
	var targetUid = msg.targetUid
	var value = msg.value
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.admin.changeDiamond(null,adminUid,targetUid,value,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//根据手机号查询uid
handler.getPhoneByUid = function(msg,session,next) {
	var adminUid = session.get("uid")
	var phone = msg.phone
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.admin.getPhoneByUid(null,phone,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//获取管理员操作记录
handler.getOperatingRecord = function(msg,session,next) {
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.admin.getOperatingRecord(null,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//获取玩家基础信息
handler.getBasicInfo = function(msg,session,next) {
	var targetUid = msg.targetUid
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.admin.getBasicInfo(null,targetUid,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//管理员获取待发货列表
handler.getAllWaitDeliverys = function(msg,session,next) {
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.award.getAllWaitDeliverys(null,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//管理员获取已发货列表
handler.getAllFinishDeliverys = function(msg,session,next) {
	var begin = msg.begin
	if(!begin || typeof(begin) != "number" || parseInt(begin) != begin){
		begin = 1
	}
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.award.getAllFinishDeliverys(null,begin,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//管理员确认发货
handler.submitDeliverys = function(msg,session,next) {
	var deliveryId = msg.deliveryId
	var LogisticCode = msg.LogisticCode
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.award.submitDeliverys(null,deliveryId,LogisticCode,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//管理员拒绝发货申请
handler.rejectDeliverys = function(msg,session,next) {
	var deliveryId = msg.deliveryId
	var reason = msg.reason
	local.check(session,function(flag){
		if(flag){
			Handler.app.rpc.db.award.rejectDeliverys(null,deliveryId,reason,function(flag,data){
				next(null,{flag : flag,data : data})
			})
		}else{
			next(null,{flag : false})
		}
	})
}
//权限检查
local.check = function(session,cb) {
	var uid = session.get("uid")
	if(!uid){
		console.log("uid : "+uid)
		cb(false)
		return
	}
	//权限验证
	Handler.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
		console.log("data : "+data)
		if(data >= 5){
			cb(true)
		}else{
			cb(false)
		}
	})
}